package com.af.monitorModule.generator.service.impl;

import com.af.monitorModule.MonitorModuleApplication;
import com.af.monitorModule.generator.entity.Business;
import com.af.monitorModule.generator.entity.Monitor;
import com.af.monitorModule.generator.entity.VO.MonitorVO;
import com.af.monitorModule.generator.mapper.BusinessMapper;
import com.af.monitorModule.generator.service.BusinessService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyz_4dou
 * @since 2022-02-21
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;

    private static final Logger logger = LoggerFactory.getLogger(BusinessServiceImpl.class);

    @SneakyThrows
    @Override
    public JSONObject selectPage(int page, int limit, String search) {
        JSONObject condition = JSONObject.parseObject(search);
        Page<Business> p = new Page<Business>(page,limit);
        QueryWrapper<Business> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id",condition.getString("company"));
        wrapper.eq("modular_name",condition.getString("modular"));
        String times = condition.getString("time");
        String response = condition.getString("response");
        if (times.length() > 0){
            String betime[] = times.split(" ~ ");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            wrapper.ge("start_time",simpleDateFormat.parse(betime[0]).getTime());
            wrapper.le("start_time",simpleDateFormat.parse(betime[1]).getTime());
        }else {
            wrapper.ge("start_time",LocalDateTime.now().plusHours(-24).toInstant(ZoneOffset.of("+8")).toEpochMilli());
        }
        if (response.length() > 0){
            switch (response) {
                case "1":
                    wrapper.lt("const_time", 2000);
                    break;
                case "2":
                    wrapper.between("const_time", 2000,6000);
                    break;
                case "3":
                    wrapper.gt("const_time", 6000);
                    break;
                default:
                    logger.error("参数错误");
            }
        }
        Page<Business> businessPage = businessMapper.selectPage(p, wrapper);
        JSONObject res = new JSONObject();
        res.put("data",businessPage.getRecords());
        res.put("code",0);
        res.put("count",businessPage.getTotal());
        res.put("msg","日志详细信息");
        return res;
    }

    @Override
    public JSONObject allStatistics(int companyId, String modularName, Long btime, Long etime,Long min,Long max) {
        return new JSONObject(){{
            put("time",getResponseTime(companyId,modularName,btime,etime,min,max));
            put("error",getResponseError(companyId,modularName,btime,etime,min,max));
            put("type",getResponseType(companyId,modularName,btime,etime,min,max));
        }};
    }

    public List<JSONObject> getResponseTime(int companyId, String modularName, Long btime, Long etime,Long min,Long max){
        return businessMapper.regularStatistics(companyId,modularName,btime,etime,min,max);
    }

    public List<JSONObject> getResponseError(int companyId, String modularName, Long btime, Long etime,Long min,Long max){
        return businessMapper.regularError(companyId,modularName,btime,etime,min,max);
    }

    public List<JSONObject> getResponseType(int companyId, String modularName, Long btime, Long etime,Long min,Long max){
        return businessMapper.regularType(companyId,modularName,btime,etime,min,max);
    }
}
